package com.heny.demo.strategy.file;

import com.alibaba.excel.EasyExcel;
import com.heny.demo.constants.EPSGConst;
import com.heny.demo.entity.TurnPoint;
import com.heny.demo.util.GeometryUtil;
import org.locationtech.jts.geom.Point;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Xlsx文件策略实现类
 *
 * @author 邢益萌
 * {@code @date} 2025/9/12 18:16
 */
public class XlsxFileImportStrategy implements FileImportStrategy {

    @Override
    public List<TurnPoint> importFile(InputStream inputStream) {
        //同步读取Excel数据
        //适合excel文件不大的情况，若文件过大可使用监听器异步读取
        //本次使用简单的同步读取
        List<Map<Integer, String>> rawData = EasyExcel.read(inputStream)
                .headRowNumber(0)
                .sheet()
                .doReadSync();

        List<TurnPoint> points = new ArrayList<>();
        for (Map<Integer, String> row : rawData) {
            String index = row.get(0).trim();
            double x = Double.parseDouble(row.get(1));
            double y = Double.parseDouble(row.get(2));
            //构建坐标
            Point point = GeometryUtil.createPoint(x, y, EPSGConst.EPSG_4547);
            //转换为目标坐标
            Point converted = GeometryUtil.transform(point, EPSGConst.EPSG_4547, EPSGConst.EPSG_4326);
            //赋值
            TurnPoint tp = new TurnPoint();
            tp.setIndex(index);
            tp.setCoordinate(converted);

            points.add(tp);
        }
        return points;
    }
}
